<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="auto" label-position="left">
			<el-row :gutter="20">
				<el-col :span="24">
					<el-form-item label="标题" prop="name">
						<el-input v-model="form.name" clearable :maxlength="80"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="标识" prop="mark">
						<el-input v-model="form.mark" clearable :maxlength="30"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="排序" prop="sort">
						<el-input-number v-model="form.sort" :min="0" :max="255" :controls="false"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<sc-editor v-model="form.content" placeholder="请输入协议内容" :height="400" :disabled="mode=='show'">
					</sc-editor>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import { defineAsyncComponent } from 'vue';
	const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));

	export default {
		emits: ['success', 'closed'],
		components: {
			scEditor
		},
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				parentSearch: {},
				//表单数据
				form: {
					id:0,
					name: "",
					mark: "",
					sort: 0,
					content: "",
				},
				//验证规则
				rules: {
					name: [
						{required: true, message: '请输入名称'}
					],
					mark: [
						{required: true, message: '请输入标识'}
					],
					sort: [
						{required: true, message: '请输入排序序号'}
					],
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode = 'add', search = null){
				this.mode = mode;
				if(search) {
					this.parentSearch = search
				}
				this.visible = true;
				return this
			},
			//表单提交方法
			submit() {
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.platform.agreement.edit.post(this.form);
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.mode, this.parentSearch)
							this.visible = false;
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data) {
				Object.assign(this.form, data)
			}
		}
	}
</script>


